package lg.minecraft.plugin.pokedex.gui;

import com.cobblemon.mod.common.pokemon.Species;
import lg.minecraft.plugin.library.inventory.CancelInventoryContext;
import lg.minecraft.plugin.library.inventory.ClickInventoryContext;
import lg.minecraft.plugin.library.inventory.InventoryContext;
import lg.minecraft.plugin.library.inventory.LGInventory;
import lg.minecraft.plugin.pokedex.Generations;
import lg.minecraft.plugin.pokedex.LGPokedex;
import lg.minecraft.plugin.pokedex.PokedexPlayer;
import lg.minecraft.plugin.pokedex.config.GenConfig;
import lg.minecraft.plugin.pokedex.config.GuiItemConfig;
import lg.minecraft.plugin.pokedex.config.RewardConfig;
import lg.minecraft.plugin.pokedex.util.CommandUtil;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;

import java.util.List;
import java.util.Objects;

/**
 * @author langle__
 * @version 1.0
 */
public class GenInventory extends LGInventory {

    private static final int[] GLASS_ARRAY = {0, 8, 9, 17, 18, 26, 27, 35, 36, 44, 45, 46, 47, 48, 50, 51, 52, 53};

    private int total = 0;
    private int gen1 = 0;
    private int gen2 = 0;
    private int gen3 = 0;
    private int gen4 = 0;
    private int gen5 = 0;
    private int gen6 = 0;
    private int gen7 = 0;
    private int gen8 = 0;
    private int gen9 = 0;

    public GenInventory() {
        super(6, "全世代");
    }

    @Override
    public void onInventoryOpen(CancelInventoryContext context) {
        Player player = context.getPlayer();
        updateSpeciesCount(player);
        PokedexPlayer pokedexPlayer = LGPokedex.getPokedexPlayerManger().getPokedexPlayer(player);

        for (int slot : GLASS_ARRAY) inventory.setItem(slot, GuiItemConfig.INSTANCE.getBorder());

        inventory.setItem(2, GuiItemConfig.INSTANCE.getGen1(gen1, pokedexPlayer.isGenerationRewardClaimed(Generations.GEN1)));
        inventory.setItem(4, GuiItemConfig.INSTANCE.getGen2(gen2, pokedexPlayer.isGenerationRewardClaimed(Generations.GEN2)));
        inventory.setItem(6, GuiItemConfig.INSTANCE.getGen3(gen3, pokedexPlayer.isGenerationRewardClaimed(Generations.GEN3)));
        inventory.setItem(20, GuiItemConfig.INSTANCE.getGen4(gen4, pokedexPlayer.isGenerationRewardClaimed(Generations.GEN4)));
        inventory.setItem(22, GuiItemConfig.INSTANCE.getGen5(gen5, pokedexPlayer.isGenerationRewardClaimed(Generations.GEN5)));
        inventory.setItem(24, GuiItemConfig.INSTANCE.getGen6(gen6, pokedexPlayer.isGenerationRewardClaimed(Generations.GEN6)));
        inventory.setItem(38, GuiItemConfig.INSTANCE.getGen7(gen7, pokedexPlayer.isGenerationRewardClaimed(Generations.GEN7)));
        inventory.setItem(40, GuiItemConfig.INSTANCE.getGen8(gen8, pokedexPlayer.isGenerationRewardClaimed(Generations.GEN8)));
        inventory.setItem(42, GuiItemConfig.INSTANCE.getGen9(gen9, pokedexPlayer.isGenerationRewardClaimed(Generations.GEN9)));
        inventory.setItem(49, GuiItemConfig.INSTANCE.getAllGen(gen1, gen2, gen3, gen4, gen5, gen6, gen7, gen8, gen9, total, pokedexPlayer.isGenerationRewardClaimed(Generations.ALL_GEN)));
    }

    @Override
    public void onInventoryClick(ClickInventoryContext context) {
        context.setCancelled(true);
        Player player = context.getPlayer();
        ClickType click = context.getClick();
        int slot = context.getSlot();

        int gen1Count = GenConfig.INSTANCE.getGen1().size();
        int gen2Count = GenConfig.INSTANCE.getGen2().size();
        int gen3Count = GenConfig.INSTANCE.getGen3().size();
        int gen4Count = GenConfig.INSTANCE.getGen4().size();
        int gen5Count = GenConfig.INSTANCE.getGen5().size();
        int gen6Count = GenConfig.INSTANCE.getGen6().size();
        int gen7Count = GenConfig.INSTANCE.getGen7().size();
        int gen8Count = GenConfig.INSTANCE.getGen8().size();
        int gen9Count = GenConfig.INSTANCE.getGen9().size();
        int maxCount = GenConfig.INSTANCE.getAllGen().size();

        PokedexPlayer pokedexPlayer = LGPokedex.getPokedexPlayerManger().getPokedexPlayer(player);
        if (slot == 2) {
            if (click.isLeftClick()) {
                new PokedexInventory(Generations.GEN1).openInventory(player);
                player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
            } else if (click.isRightClick() && ((gen1Count - gen1) == 0) && !pokedexPlayer.isGenerationRewardClaimed(Generations.GEN1)) {
                List<String> commands = RewardConfig.INSTANCE.getGen1().commands();
                CommandUtil.commandExecute(player, commands);
                pokedexPlayer.setGenerationRewardClaimed(Generations.GEN1, true);
                onInventoryOpen(context);
            }
        } else if (slot == 4) {
            if (click.isLeftClick()) {
                new PokedexInventory(Generations.GEN2).openInventory(player);
                player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
            } else if (click.isRightClick() && ((gen2Count - gen2) == 0) && !pokedexPlayer.isGenerationRewardClaimed(Generations.GEN2)) {
                List<String> commands = RewardConfig.INSTANCE.getGen2().commands();
                CommandUtil.commandExecute(player, commands);
                pokedexPlayer.setGenerationRewardClaimed(Generations.GEN2, true);
                onInventoryOpen(context);
            }
        } else if (slot == 6) {
            if (click.isLeftClick()) {
                new PokedexInventory(Generations.GEN3).openInventory(player);
                player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
            } else if (click.isRightClick() && ((gen3Count - gen3) == 0) && !pokedexPlayer.isGenerationRewardClaimed(Generations.GEN3)) {
                List<String> commands = RewardConfig.INSTANCE.getGen3().commands();
                CommandUtil.commandExecute(player, commands);
                pokedexPlayer.setGenerationRewardClaimed(Generations.GEN3, true);
                onInventoryOpen(context);
            }
        } else if (slot == 20) {
            if (click.isLeftClick()) {
                new PokedexInventory(Generations.GEN4).openInventory(player);
                player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
            } else if (click.isRightClick() && ((gen4Count - gen4) == 0) && !pokedexPlayer.isGenerationRewardClaimed(Generations.GEN4)) {
                List<String> commands = RewardConfig.INSTANCE.getGen4().commands();
                CommandUtil.commandExecute(player, commands);
                pokedexPlayer.setGenerationRewardClaimed(Generations.GEN4, true);
                onInventoryOpen(context);
            }
        } else if (slot == 22) {
            if (click.isLeftClick()) {
                new PokedexInventory(Generations.GEN5).openInventory(player);
                player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
            } else if (click.isRightClick() && ((gen5Count - gen5) == 0) && !pokedexPlayer.isGenerationRewardClaimed(Generations.GEN5)) {
                List<String> commands = RewardConfig.INSTANCE.getGen5().commands();
                CommandUtil.commandExecute(player, commands);
                pokedexPlayer.setGenerationRewardClaimed(Generations.GEN5, true);
                onInventoryOpen(context);
            }
        } else if (slot == 24) {
            if (click.isLeftClick()) {
                new PokedexInventory(Generations.GEN6).openInventory(player);
                player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
            } else if (click.isRightClick() && ((gen6Count - gen6) == 0) && !pokedexPlayer.isGenerationRewardClaimed(Generations.GEN6)) {
                List<String> commands = RewardConfig.INSTANCE.getGen6().commands();
                CommandUtil.commandExecute(player, commands);
                pokedexPlayer.setGenerationRewardClaimed(Generations.GEN6, true);
                onInventoryOpen(context);
            }
        } else if (slot == 38) {
            if (click.isLeftClick()) {
                new PokedexInventory(Generations.GEN7).openInventory(player);
                player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
            } else if (click.isRightClick() && ((gen7Count - gen7) == 0) && !pokedexPlayer.isGenerationRewardClaimed(Generations.GEN7)) {
                List<String> commands = RewardConfig.INSTANCE.getGen7().commands();
                CommandUtil.commandExecute(player, commands);
                pokedexPlayer.setGenerationRewardClaimed(Generations.GEN7, true);
                onInventoryOpen(context);
            }
        } else if (slot == 40) {
            if (click.isLeftClick()) {
                new PokedexInventory(Generations.GEN8).openInventory(player);
                player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
            } else if (click.isRightClick() && ((gen8Count - gen8) == 0) && !pokedexPlayer.isGenerationRewardClaimed(Generations.GEN8)) {
                List<String> commands = RewardConfig.INSTANCE.getGen8().commands();
                CommandUtil.commandExecute(player, commands);
                pokedexPlayer.setGenerationRewardClaimed(Generations.GEN8, true);
                onInventoryOpen(context);
            }
        } else if (slot == 42) {
            if (click.isLeftClick()) {
                new PokedexInventory(Generations.GEN9).openInventory(player);
                player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
            } else if (click.isRightClick() && ((gen9Count - gen9) == 0) && !pokedexPlayer.isGenerationRewardClaimed(Generations.GEN9)) {
                List<String> commands = RewardConfig.INSTANCE.getGen9().commands();
                CommandUtil.commandExecute(player, commands);
                pokedexPlayer.setGenerationRewardClaimed(Generations.GEN9, true);
                onInventoryOpen(context);
            }
        } else if (slot == 49) {
            if (click.isLeftClick()) {
                new PokedexInventory(Generations.ALL_GEN).openInventory(player);
                player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
            } else if (click.isRightClick() && ((maxCount - total) == 0) && !pokedexPlayer.isGenerationRewardClaimed(Generations.ALL_GEN)) {
                List<String> commands = RewardConfig.INSTANCE.getAllGen().commands();
                CommandUtil.commandExecute(player, commands);
                pokedexPlayer.setGenerationRewardClaimed(Generations.ALL_GEN, true);
                onInventoryOpen(context);
            }
        }
    }

    @Override
    public void onInventoryClose(InventoryContext context) {
    }

    public void updateSpeciesCount(Player player) {
        GenConfig gen = GenConfig.INSTANCE;
        PokedexPlayer pokedexPlayer = LGPokedex.getPokedexPlayerManger().getPokedexPlayer(player);
        gen1 = countTrackedSpecies(gen.getGen1(), pokedexPlayer);
        gen2 =  countTrackedSpecies(gen.getGen2(), pokedexPlayer);
        gen3 = countTrackedSpecies(gen.getGen3(), pokedexPlayer);
        gen4 = countTrackedSpecies(gen.getGen4(), pokedexPlayer);
        gen5 = countTrackedSpecies(gen.getGen5(), pokedexPlayer);
        gen6 = countTrackedSpecies(gen.getGen6(), pokedexPlayer);
        gen7 = countTrackedSpecies(gen.getGen7(), pokedexPlayer);
        gen8 = countTrackedSpecies(gen.getGen8(), pokedexPlayer);
        gen9 = countTrackedSpecies(gen.getGen9(), pokedexPlayer);
        total = gen1 + gen2 + gen3 + gen4 + gen5 + gen6 + gen7 + gen8 + gen9;
    }

    public int countTrackedSpecies(List<Species> speciesList, PokedexPlayer pokedexPlayer) {
        if (speciesList == null) return 0;
        return (int) speciesList.stream()
                .filter(Objects::nonNull)
                .filter(pokedexPlayer::hasTrackedSpecies)
                .count();
    }

}
